<template>
	<view class="page-main">
		<liu-slide-questions :dataList="lists" @submit="subData"></liu-slide-questions>
	</view>
</template>

<script>
	import {
		getUserTestPaper,
		addUserTestPaper
	} from '../../api/Teacher.js';
	import {
		userinfo,
	} from '../../api/Mine.js';
	export default {
		data() {
			return {
				list: [{
						id: '1',
						title: '这是一道单选题',
						problemType: 'SINGLE',
						children: [{
								alias: 'A',
								answer: '选项1',
								isSelect: 0 //这个是用来回显的字段(0:未选中；1:选中)(可不传)
							},
							{
								alias: 'B',
								answer: '选项2',
								isSelect: 0 //这个是用来回显的字段(0:未选中；1:选中)(可不传)
							},
							{
								alias: 'C',
								answer: '选项3',
								isSelect: 0 //这个是用来回显的字段(0:未选中；1:选中)(可不传)
							}
						]
					},
					{
						id: '2',
						title: '这是一道多选题',
						problemType: 'MULTY',
						children: [{
								alias: 'A',
								answer: '选项1',
								isSelect: 0 //这个是用来回显的字段(0:未选中；1:选中)(可不传)
							},
							{
								alias: 'B',
								answer: '选项2',
								isSelect: 0 //这个是用来回显的字段(0:未选中；1:选中)(可不传)
							},
							{
								alias: 'C',
								answer: '选项3',
								isSelect: 0 //这个是用来回显的字段(0:未选中；1:选中)(可不传)
							},
							{
								alias: 'D',
								answer: '选项4',
								isSelect: 0 //这个是用来回显的字段(0:未选中；1:选中)(可不传)
							}
						]
					},
					{
						id: '3',
						title: '这是一道简答题',
						problemType: 'QUESTION',
						userAnswer: '' //这个是用来回显的字段(可不传)
					}
				], //问题列表数据
				lists: [],
				info:{},
				req: {
					total: '', //总题数
					correct: '' //答对题数
				}
			};
		},
		onLoad(option) {
			this.getUserTestPaper();
		},
		onReady(){
			this.userinfo();
		},
		methods: {
			/**
			@method 老师详情
			**/
			async userinfo() {
				try {
					const res = await userinfo();
					uni.setNavigationBarTitle({
						title: `${res.data.userinfo.student_type_id == 3 ? '成人' : '孩童'}测试问卷`,
						complete(err){
							console.log(err);
						}
					});
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
			},
			//点击提交-问卷
			async subData(data) {
				console.log(data);
				try {
					let arrs = new Array();
					let count = data.filter((item) => item.userAnswer.state === '1').length;
					let NoCount = data.filter((item) => item.userAnswer.state !== '1').length;
					console.log(count, 'arrsarrsarrs');
					const res = await addUserTestPaper({
						total: data.length, //总题数
						correct: count
					});
					if (res.code == 1 || res.code == '1') {
						uni.showToast({
							title: '提交成功！',
							icon: 'none',
							duration: 2000
						});
						uni.$emit('confirm', {
							yes: count,
							no: NoCount,
							level: res.data.level.title
						});

					}
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
				console.log('提交的数据：' + JSON.stringify(data));
			},
			/**
				@method 获取问卷题目
				**/
			async getUserTestPaper() {
				try {
					const res = await getUserTestPaper({
						shop_id: 1
					});
					this.lists = [...res.data.list];
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
			}
		}
	};
</script>